package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"user-profile/model/mysql"

	"user-profile/userrpc/internal/svc"
	"user-profile/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改
func (l *UpdateLogic) Update(in *userrpc.UpdateRequest) (*userrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line
	var users mysql.User
	mysql.DB.Debug().Where("id = ?", in.Id).Find(&users)
	if users.ID == 0 {
		return nil, fmt.Errorf("请登录账号")
	}
	user := mysql.User{
		Model: gorm.Model{ID: uint(in.Id)},
		Img:   in.Img,
		Name:  in.Name,
		Time:  in.Time,
		Sex:   in.Sex,
		Text:  in.Text,
	}
	err := mysql.DB.Debug().Updates(&user).Error
	if err != nil {
		return nil, fmt.Errorf("修改失败")
	}
	return &userrpc.UpdateResponse{
		Id: int64(user.ID),
	}, nil
}
